package org.easy.byteasr;


import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;


@Configuration
@EnableConfigurationProperties({ByteAsrProperties.class})
@ConditionalOnProperty(prefix = ByteAsrProperties.PREFIX, name = "enabled", havingValue = "true")
public class ByteAsrAutoConfiguration {

    @Resource
    private ByteAsrProperties byteAsrProperties;

    @Bean
    public ByteRealtimeAsrV2Template byteRealtimeAsrV2Template() {
        return new ByteRealtimeAsrV2Template(byteAsrProperties.getAppid(), byteAsrProperties.getAccessToken(), byteAsrProperties.getCluster());
    }

    @Bean
    public ByteRealtimeAsrV3Template byteRealtimeAsrV3Template() {
        return new ByteRealtimeAsrV3Template(new ByteRealtimeAsrV3Client(byteAsrProperties.getAppid(), byteAsrProperties.getAccessToken()));
    }

    public static void main(String[] args) throws IOException {
        String appId = "2262349642";
        String token = "4sAKzMMKquDhRNwb2ol0wQ02NPshlAE6";
        File file = new File("C:\\Users\\Administrator\\Desktop\\16k.wav");
        ByteRealtimeAsrV3Template template = new ByteRealtimeAsrV3Template(new ByteRealtimeAsrV3Client(appId, token));

        //sampleRate16000  sampleSizeInBits16 channels1
//        AudioFormat audioFormat = new AudioFormat(16000, 16, 1);
//        String sessionId = template.initGetSessionId("test", audioFormat, new ByteAsrWebSocketListener() {
//            @Override
//            public void onOpen(WebSocket webSocket, Response response) {
//
//            }
//
//            @Override
//            public void onMessage(WebSocket webSocket, ByteAudioInfoResponse byteAudioInfoResponse) {
//                System.out.println(byteAudioInfoResponse.getResult().getText());
//            }
//        });
//
//
//        template.sendAudioOnlyWithSeq(sessionId, Files.newInputStream(file.toPath()));
//
//
//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
    }


}
